package service

import (
	"SecKill/common/define"
	"SecKill/common/models"
	"SecKill/common/response"
	"context"
	"encoding/json"
	"github.com/gin-gonic/gin"
)

func QueryTypeList(c *gin.Context) {
	var shopTypeVoList []models.ShopTypeVo
	result, err := models.RDb.Get(context.Background(), define.CACHE_SHOPTYPE_KEY).Result()
	if err != nil {
		models.Db.Model(new(models.ShopTypeVo)).Order("sort asc").Find(&shopTypeVoList)
		shopTypeVoListJson, _ := json.Marshal(shopTypeVoList)
		models.RDb.Set(context.Background(), define.CACHE_SHOPTYPE_KEY, shopTypeVoListJson, define.CACHE_SHOPTYPE_TTL)
		response.ResponseOKWithData(c, "Success", shopTypeVoList, len(shopTypeVoList))
		return
	}
	models.RDb.Expire(context.Background(), define.CACHE_SHOPTYPE_KEY, define.CACHE_SHOPTYPE_TTL)
	json.Unmarshal([]byte(result), &shopTypeVoList)
	response.ResponseOKWithData(c, "Success", shopTypeVoList, len(shopTypeVoList))
	return
}
